package com.tech.zhidengxian.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tech.zhidengxian.mapper.CosMapper;
import com.tech.zhidengxian.model.dataobject.CosDO;
import com.tech.zhidengxian.model.dto.CosDTO;
import com.tech.zhidengxian.service.api.CosService;
import com.tech.zhidengxian.util.JacksonUtils;
import com.tech.zhidengxian.util.TimeUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CosServiceImpl implements CosService {
    @Resource
    private CosMapper ossMapper;

    @Override
    public List<CosDTO> queryOssList() {
        List<CosDO> ossDOList = ossMapper.queryOssList();
        List<CosDTO> cosDTOList = JacksonUtils.convertList(ossDOList, CosDTO.class);
        return cosDTOList;
    }

    @Override
    public List<CosDTO> queryBannerList() {
        LambdaQueryWrapper<CosDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CosDO::getFileBizType, "banner");
        return JacksonUtils.convertList(ossMapper.selectList(queryWrapper), CosDTO.class);
    }

    @Override
    public Long uploadFile(CosDTO cosDTO) {
        CosDO ossDO = JacksonUtils.convert(cosDTO, CosDO.class);
        ossDO.setCreateTime(TimeUtil.currentDate());
        ossMapper.insert(ossDO);
        return ossDO.getId();
    }

    @Override
    public int deleteByName(String name) {
        return ossMapper.deleteByName(name);
    }

    @Override
    public int updateById(CosDTO cosDTO) {
        CosDO cosDO = JacksonUtils.convert(cosDTO, CosDO.class);
        return ossMapper.updateById(cosDO);
    }


}
